package order

import (
	"net/http"

	"github.com/xinliangnote/go-gin-api/internal/code"
	"github.com/xinliangnote/go-gin-api/internal/pkg/core"
	"github.com/xinliangnote/go-gin-api/internal/pkg/validation"
	"github.com/xinliangnote/go-gin-api/internal/services/order"

	"github.com/spf13/cast"
)

type detailRequest struct{
	Id string `uri:"id"` // HashID
}

type detailResponse struct{
	OrderNo                string `json:"order_no"`          // 订单编号		
	OrderFee             int32 `json:"order_fee"`       // 订单金额
}

// Detail 取消订单
// @Summary 取消订单
// @Description 取消订单
// @Tags API.order
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Param Request body detailRequest true "请求信息"
// @Success 200 {object} detailResponse
// @Failure 400 {object} code.Failure
// @Router /api/order/{id} [get]
func (h *handler) Detail() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(detailRequest)
		res := new(detailResponse)
		if err := ctx.ShouldBindURI(req); err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusBadRequest,
				code.ParamBindError,
				validation.Error(err)).WithError(err),
			)
			return
		}

		ids, err := h.hashids.HashidsDecode(req.Id)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusBadRequest,
				code.HashIdsDecodeError,
				code.Text(code.HashIdsDecodeError)).WithError(err),
			)
			return
		}

		searchOneData := new(order.SearchOneData)
		searchOneData.Id = cast.ToInt32(ids[0])

		info, err := h.orderService.Detail(ctx, searchOneData)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusBadRequest,
				code.CronDetailError,
				code.Text(code.CronDetailError)).WithError(err),
			)
			return
		}

		res.OrderNo = info.OrderNo
		res.OrderFee = info.OrderFee
		
		ctx.Payload(res)
	}
}
